package com.example.thornchg.yc_plane_game;

import org.cocos2d.actions.CCScheduler;
import org.cocos2d.actions.base.CCRepeatForever;
import org.cocos2d.actions.instant.CCCallFunc;
import org.cocos2d.actions.instant.CCHide;
import org.cocos2d.actions.interval.CCAnimate;
import org.cocos2d.actions.interval.CCMoveBy;
import org.cocos2d.actions.interval.CCMoveTo;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.nodes.CCAnimation;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCNode;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCSpriteFrame;
import org.cocos2d.sound.SoundEngine;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGSize;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

public class Hero extends CCSprite{
    private CGSize winSize;
    private float limit[];  //玩家战机边界   左 右 上 下
    private List<CCSprite> bullets;
    private int attackPower;   //攻击力


    public Hero() {
        super("hero/hero1.png");
        winSize = CCDirector.sharedDirector().getWinSize();
        setPosition(winSize.getWidth() / 2, winSize.getHeight() / 6);
        bullets=new ArrayList<>();
        // 初始化玩家战机边界
        limit=new float[]{
                this.getBoundingBox().size.getWidth()/2,
                winSize.getWidth()-this.getBoundingBox().size.getWidth()/2,
                winSize.getHeight()-this.getBoundingBox().size.getHeight()/2,
                this.getBoundingBox().size.getHeight()/2,
        };
        this.attackPower=100;
    }
    public void shootBullet(){
        CCScheduler.sharedScheduler().schedule("createHeroBullet",this,
                0.1f,false);
    }
    public void createHeroBullet(float t) {
        CCSprite ccSprite_hero_bullet=null;
        for (CCSprite bullet : bullets) {
            if (!bullet.getVisible()){
                ccSprite_hero_bullet=bullet;
                ccSprite_hero_bullet.setVisible(true);
                break;
            }
        }
        if (ccSprite_hero_bullet==null){
            ccSprite_hero_bullet=CCSprite.sprite("hero_bullet1.png");
            ccSprite_hero_bullet.setAnchorPoint(0.5f,0);
            bullets.add(ccSprite_hero_bullet);
            getParent().addChild(ccSprite_hero_bullet);
        }
        ccSprite_hero_bullet.setPosition(this.getPosition().x,
                this.getPosition().y+this.getBoundingBox().size.getHeight()/2+5);

        /*CCMoveBy ccMoveBy=CCMoveBy.action(.1f,
                ccp(0,100));*/


        CCMoveTo ccMoveBy=CCMoveTo.action(.5f,ccp(ccSprite_hero_bullet.getPosition().x,
                winSize.getHeight()));
//        CCCallFunc ccCallFunc=CCCallFunc.action(this,"bulletMoveByEnd");
        CCHide ccHide=CCHide.action();
        CCSequence ccSequence=CCSequence.actions(ccMoveBy,ccHide);
//        CCRepeatForever ccRepeatForever=CCRepeatForever.action(ccSequence);
        ccSprite_hero_bullet.runAction(ccSequence);
    }
    /*public void bulletMoveByEnd(){
        Iterator<CCSprite> iterator = bullets.iterator();
        while (iterator.hasNext()){
            CCSprite next = iterator.next();
            if (next.getPosition().y>=winSize.getHeight()){
                next.stopAllActions();
                next.setVisible(false);
            }
        }
    }*/
    public void move(CGPoint distance){

        CGPoint cgPoint1 = ccpAdd(this.getPosition(), distance);
        if (cgPoint1.y>limit[2]){
            cgPoint1.set(cgPoint1.x,limit[2]);
        }else if (cgPoint1.y<limit[3]){
            cgPoint1.set(cgPoint1.x,limit[3]);
        }
        if (cgPoint1.x<limit[0]){
            cgPoint1.set(limit[0],cgPoint1.y);
        }else if (cgPoint1.x>limit[1]){
            cgPoint1.set(limit[1],cgPoint1.y);
        }
        this.setPosition(cgPoint1);
    }

    public List<CCSprite> getBullets() {
        return bullets;
    }
    public void showExplosion(CCNode ccNode){
        SoundEngine.sharedEngine().playEffect(CCDirector.theApp,R.raw.game_over);
        CCScheduler.sharedScheduler().unschedule("createHeroBullet",this);
        ArrayList<CCSpriteFrame> ccSpriteFrames=new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            CCSpriteFrame ccSpriteFrame=CCSprite.spriteWithSpriteFrameName(String.format(Locale.CHINA,
                    "hero/hero_blowup_n%d.png",i+1)).displayedFrame();
            ccSpriteFrames.add(ccSpriteFrame);
        }
        CCAnimation ccAnimation=CCAnimation.animationWithFrames(ccSpriteFrames,.1f);
        CCAnimate ccAnimate=CCAnimate.action(ccAnimation,false);
        CCHide ccHide=CCHide.action();
        CCCallFunc ccCallFunc=CCCallFunc.action(ccNode,"gameOver");
        CCSequence ccSequence=CCSequence.actions(ccAnimate,ccHide,ccCallFunc);
        this.runAction(ccSequence);
    }
    public void resume(){
        setDisplayFrame(CCSprite.spriteWithSpriteFrameName(
                "hero/hero1.png").displayedFrame());
        setPosition(winSize.getWidth() / 2, winSize.getHeight() / 6);
        setVisible(true);

    }

    public int getAttackPower() {
        return attackPower;
    }

    public void setAttackPower(int attackPower) {
        this.attackPower = attackPower;
    }
}
